import { createRouter, createWebHistory } from 'vue-router';
import { getToken, getIsLogin } from '@/utils/storage'
import { useUserStore } from '@/stores/user'

const whiteList = ['/login', '/check', '/confirm-dialog']

const router = createRouter({
    history: createWebHistory('/mobile/'),
    // history: createWebHistory(""),
    routes: [
        {
            path: '/',
            redirect: '/login', // 重定向
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/view/login/index.vue'),
            meta: {
                topShow: false, // 是否显示顶部导航栏
                searchInput: false, // 是否显示搜索框
                keepAlive: false // 是否缓存页面
            }
        },
        {
            path: '/check',
            name: 'check',
            component: () => import('@/view/check/index.vue'),
            meta: {
                topShow: false,
                searchInput: false,
                keepAlive: false // 是否缓存页面

            }
        },
        {
            path: '/menu/worksheets',
            name: 'worksheets',
            component: () => import('@/view/menu/worksheets/index.vue'),
            meta: {
                topShow: true,
                searchInput: true,
                keepAlive: false // 是否缓存页面

            }
        },
        {
            path: '/message',
            name: 'message',
            component: () => import('@/view/message/index.vue'),
            meta: {
                topShow: true,
                searchInput: false,
                keepAlive: false // 是否缓存页面
            }
        },
        {
            path: '/create-new-work-order',
            name: 'createNewWorkOrder',
            component: () => import('@/view/menu/worksheets/components/CreateNewWokOrder.vue'),
            meta: {
                topShow: false,
                searchInput: false,
                keepAlive: false // 是否缓存页面
            }
        },
        {
            path: '/work-order-detail',
            name: 'WorkOrderDetail',
            component: () => import('@/view/menu/worksheets/components/WorkOrderDetail.vue'),
            meta: {
                topShow: false,
                searchInput: false,
                keepAlive: false // 是否缓存页面
            }
        },

        {
            path: '/ai-retrieval',
            name: 'aiRetrieval',
            component: () => import('@/view/aiRetrieval/index.vue'),
            meta: {
                topShow: true,
                searchInput: false,
                keepAlive: true // 是否缓存页面
            }
        },

        {
            path: '/confirm-dialog',
            name: 'confirmDialog',
            component: () => import('@/components/ConfirmDialog/index.vue'),
            meta: {
                topShow: false,
                searchInput: false,
                keepAlive: false // 是否缓存页面
            }
        },
        {
            path: '/:pathMatch(.*)*',
            component: () => import('@/view/error/index.vue'),
            beforeEnter: (to, from, next) => {
                if (to.fullPath === '/static/common/faq.html') {
                    next(false)
                } else {
                    next
                }
            }
        },
    ]
});

// router.beforeEach((to, from, next) => {

//     const store = useUserStore()

//     //判断当前设备是移动端还是pc端
//     if (!/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
//         // pc端
//         window.location.href = "http://10.11.228.185";
//         next()
//         return
//     }

//     if (getToken()) {
//         if (whiteList.indexOf(to.path) >= 0) {
//             if (store.tabActive === '/menu/worksheets') {
//                 next('/menu/worksheets');
//             } else {
//                 next(store.tabActive);
//             }
//         } else {

//             store.tabActive = to.path

//             if (!store.AppState.sidebarRouters.length) {

//                 store.getPermission()

//                 next()
//                 // next({ ...to, replace: true }) // 防止出现重定向死循环
//             } else {
//                 next()
//             }
//         }
//     } else {

//         const isLogin = to.path.indexOf('/check') >= 0 && getIsLogin()

//         if (whiteList.includes(to.path) || isLogin) {
//             next()
//         }
//         else {
//             if (to.fullPath.indexOf('/check') >= 0) {
//                 next('/login')
//             } else {
//                 next('/login')
//             }
//         }

//     }

// })
// http://10.11.228.185:5175/mobile/work-order-detail?id=202501030001
router.beforeEach((to, from, next) => {
    const store = useUserStore();
    const token = getToken(); // 保存 token 状态，避免多次调用
    const isMobile = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
    const isInWhiteList = whiteList.includes(to.path);
    const isLoginPage = to.path.indexOf('/check') >= 0 && getIsLogin();

    // 判断设备类型
    if (!isMobile) {
        window.location.href = "/"; // 重定向到 PC 端
        return next(); // 防止继续执行后续代码
    }

    // 如果用户已登录
    if (token) {
        if (to.path === '/login') {
            // 如果路径是 /mobile/，跳转到首页
            return next('/menu/worksheets'); // 假设首页是 /mobile/home
        }

        if (isInWhiteList) {
            if (store.tabActive === '/menu/worksheets') {
                return next('/menu/worksheets');
            } else {
                return next(store.tabActive);
            }
        }

        // 保存当前路径为 tabActive
        store.tabActive = to.path;

        // 如果没有加载权限，加载权限数据
        if (!store.AppState.sidebarRouters.length) {
            store.getPermission();
        }

        return next();
    }

    // 如果未登录，处理白名单路径或登录页
    if (isInWhiteList || isLoginPage) {
        return next();
    }

    return next('/login'); // 重定向到登录页
});

export default router;
